package com.menker.app.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.text.TextUtils;

import com.menker.app.biz.ItemInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by developer on 17-11-27.
 */

public class ItemDAO extends SQLiteOpenHelper {

    private static final String name = "menker";

    private static final int version = 1;

    private final String TABLE_NAME = "item";

    private final String FIELD_TIME = "time";

    private final String FIELD_DATA = "data";

    public ItemDAO(Context context) {
        super(context, name, null, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (id integer primary key autoincrement, " + FIELD_TIME + " long(20), " + FIELD_DATA + " varchar(20))");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }


    public void insertData(long time,String data) {
        SQLiteDatabase db = getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put(FIELD_TIME, time);
        cv.put(FIELD_DATA, data);
        db.insert(TABLE_NAME, null, cv);
        db.close();
    }

    public List<ItemInfo> queryData() {
        List<ItemInfo> infraredList = new ArrayList<>();
        SQLiteDatabase db = getReadableDatabase();
        String[] columns = {FIELD_TIME,FIELD_DATA};

        Cursor cursor;
        try {
            cursor = db.query(TABLE_NAME, columns, null, null, null, null, null);
            if (cursor.moveToFirst()) {
                do {
                    long time = cursor.getLong(cursor.getColumnIndex(FIELD_TIME));
                    String data = cursor.getString(cursor.getColumnIndex(FIELD_DATA));
                    infraredList.add(new ItemInfo(time,data));
                }

                while (cursor.moveToNext());
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        db.close();

        return infraredList;
    }

    public List<ItemInfo> queryData(String itemId) {
        List<ItemInfo> queryList = queryData();
        List<ItemInfo> itemList = new ArrayList<>();

        if (queryList != null && queryList.size() > 0) {
            for (ItemInfo item : queryList) {
                if (TextUtils.equals(itemId,item.id)) {
                    itemList.add(item);
                }
            }
        }

        return itemList;
    }

    public boolean deleteData(String itemId) {
        boolean deleteSuccess = false;

        SQLiteDatabase db = getReadableDatabase();

        int delete = db.delete(TABLE_NAME, FIELD_DATA + "=?", new String[]{itemId});

        if (delete > 0) {
            deleteSuccess = true;
        }

        db.close();
        return deleteSuccess;
    }

    public boolean changeData(String itemId,long time) {
        boolean changeSuccess = false;

        SQLiteDatabase db = getReadableDatabase();

        ContentValues values = new ContentValues();
        values.put(FIELD_DATA,itemId);
        values.put(FIELD_TIME,time);

        int update = db.update(TABLE_NAME, values, FIELD_DATA + "=?", new String[]{itemId});

        if (update > 0) {
            changeSuccess = true;
        }

        db.close();
        return changeSuccess;
    }

    public void clearData() {
        List<ItemInfo> itemList = queryData();

        for (ItemInfo item : itemList) {
            deleteData(item.id);
        }

    }

}
